
<template>
  <el-container>
    <el-header>
      <el-row>

        <el-col :span="16" >
          <h1>电商后台管理系统</h1>
        </el-col>
        <el-col :span="8" class="col_r">
          <a  @click.prevent="logout"  href="#" >退出</a>
        </el-col>
      </el-row>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <!-- 侧栏 -->
        <!--
          el-menu : 菜单组件
           -  default-active="2" 默认选中 默认高亮 (index:2)
           - @open="handleOpen"
           - @close="handleClose"
          el-submenu 菜单子组件 (可展开)
          el-menu-item 菜单元素 (最小单位)
          el-menu-item-group 分组
        -->
        <el-menu
          :router="true"
          :default-active="test()"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
        >
          <!-- 用户管理 -->
          <el-submenu
            :index="item.id+''"
            v-for='item in menus'
            :key='item.id'
          >
            <!-- 标题 -->
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{ item.authName }}</span>
              <!-- 菜单元素 -->
            </template>
            <el-menu-item
              v-for='item1 in item.children'
              :key='item1.id'
              :index="'/'+item1.path"
            >{{ item1.authName }}</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 子组件的出口 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
/* eslint-disable */
export default {
  data () {
    return {
      menus: []
    }
  },
  created () {
    this.loadMenusData()
  },
  methods: {
    // 退出
    async logout () {
      // 确认框
      try {
        const p = await this.$confirm("此操作将退出该账户, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })

        // 1. 清除token
        localStorage.removeItem("token");
        //2. 提示
        this.$message({
          message: "退出成功",
          type: "success",
          duration: 800
        });
        //3. 跳转到login页
        this.$router.push("/login");

      } catch (error) {
        // console.log('点击了取消');
        this.$message({
          message: "取消退出",
          type: "info",
          duration: 800
        });

      }

      // 点击确定的时候 => 走then
      // .then(() => {
      //   console.log("then- 点击确定");
      // // 1. 清除token
      // localStorage.removeItem("token");
      // //2. 提示
      // this.$message({
      //   message: "退出成功",
      //   type: "success",
      //   duration: 800
      // });
      // //3. 跳转到login页
      // this.$router.push("/login");
      // })
      // // 点击取消的时候 => catch
      // .catch(() => {
      //   // console.log("catch- 点击取消");
      // this.$message({
      //   message: "取消退出",
      //   type: "info",
      //   duration: 800
      // });
      // });
    },
    // 导航菜单打开和关闭
    handleOpen () {
      console.log("打开了");
    },
    handleClose () {
      console.log("关闭了");
    },
    // 加载菜单
    async loadMenusData () {
      let res = await this.$axios.get('menus')
      console.log(res);
      this.menus = res.data.data

    },
    test () {
      console.log('test:', this.$route.path.split('/')[1]);
      return '/' + this.$route.path.split('/')[1]
    }
  }
};
</script>

<style scoped lang='less'>
/* 外部容器 */
.el-container {
  height: 100%;
  min-width: 900px;
}

/* 头部 */
.el-header {
  background: #b3c1cd;
  padding: 0;
  h1 {
    color: #fff;
    text-align: left;
    line-height: 60px;
    font-size: 26px;
    padding-left: 10px;
  }
  .col_r {
    text-align: right;
    line-height: 60px;
    padding-right: 30px;
    a {
      color: green;
    }
  }
  .col-l {
    text-align: left;
  }
}

/* 侧栏 */
.el-aside {
  background: #545c64;
}

/* 主体 */
.el-main {
  background: #eaeef1;
}
</style>
